package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.YlBedMapper;
import com.caifu.mapper.YlFloorMapper;
import com.caifu.mapper.YlRoomMapper;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlFloor;
import com.caifu.pojo.YlRoom;
import com.caifu.service.IRoomService;
import com.caifu.util.Constance;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoomServiceImpl implements IRoomService {

    @Resource
    private YlFloorMapper floorMapper;

    @Resource
    private YlRoomMapper roomMapper;

    @Resource
    private YlBedMapper bedMapper;

    @Override
    public JSONObject queryFloorInfo(String floorNo) throws Exception {
        JSONObject result = new JSONObject();
        YlFloor floor = floorMapper.selectById(floorNo);
        if(null!=floor){
            result.put("floorNo",floor.getFloorNo());
            result.put("floorName",floor.getFloorName());
            result.put("buildingNo",floor.getBuildingNo());
            //查询房间数量
            int roomCount = roomMapper.queryTotalRoom(floorNo);
            if(roomCount>0){
                result.put("roomCount",roomCount);
            }else {
                result.put("roomCount","?");
            }
            //查询总床位数
            int totalBed = bedMapper.queryBedCountByFloorNo(floorNo,null);
            if(totalBed>0){
                result.put("totalBed",totalBed);
            }else {
                result.put("totalBed","?");
            }
            //查询空床位数
            int emptyBed = bedMapper.queryBedCountByFloorNo(floorNo+"","0");
            if(emptyBed>0){
                result.put("emptyBed",emptyBed);
            }else {
                result.put("emptyBed","?");
            }
        }
        return result;
    }

    @Override
    public JSONObject queryRoomData(String floorNo) throws Exception {
        JSONObject result = new JSONObject();
        JSONArray dataInfo = new JSONArray();
        int totalRecords = 0;
        try {
            QueryWrapper<YlRoom> roomQueryWrapper = new QueryWrapper<>();
            roomQueryWrapper.eq("FLOOR_NO",floorNo);
            roomQueryWrapper.eq("ROOM_STATE","1");
            roomQueryWrapper.orderByAsc("ROOM_NAME");
            roomQueryWrapper.orderByAsc("CREATE_TIME");
            List<YlRoom> roomList = roomMapper.selectList(roomQueryWrapper);
            if(null!=roomList&&roomList.size()>0){
                totalRecords = roomList.size();
                for (YlRoom r:roomList){
                    JSONObject data = new JSONObject();
                    data.put("roomNo",r.getRoomNo());
                    data.put("roomName",r.getRoomName());
                    data.put("roomType",r.getRoomType());
                    if("101".equals(Constance.Null2EmptyTrim(r.getRoomType()))){
                        data.put("remark","空床位");
                    }else {
                        data.put("remark",r.getRoomRemark());
                    }
                    //查询房间里的床位数
                    int totalBed = bedMapper.queryBedCountByRoomNo(r.getRoomNo()+"",null);
                    if(totalBed>0){
                        data.put("totalBed",totalBed);
                    }else {
                        data.put("totalBed","?");
                    }
                    //查询空置床位数
                    int emptyBed = bedMapper.queryBedCountByRoomNo(r.getRoomNo()+"","0");
                    if(emptyBed>0){
                        data.put("emptyBed",emptyBed);
                    }else {
                        data.put("emptyBed","?");
                    }
                    int useBed = totalBed-emptyBed;
                    if(useBed>0){
                        data.put("useBed",useBed);
                    }else {
                        data.put("useBed","?");
                    }
                    dataInfo.add(data);
                }
            }
            result.put("code","0000");
            result.put("codeDesc","查询成功");
            result.put("roomCount",totalRecords);
            result.put("data",dataInfo);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","查询失败");
            result.put("roomCount",0);
            result.put("data",null);
            return result;
        }
    }

    @Override
    public JSONObject saveRoomInfo(YlRoom room, SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        if(null==user){
            result.put("code","1001");
            result.put("codeDesc","登录超时，请重新登录");
            return result;
        }
        if(null==room){
            result.put("code","1002");
            result.put("codeDesc","房间信息为空");
            return result;
        }
        try {
            String roomName = Constance.Null2EmptyTrim(room.getRoomName());
            String floorNo =  Constance.Null2EmptyTrim(room.getFloorNo());
            //验证楼层名称是否存在
            YlRoom exitRoom = roomMapper
                    .queryRoomInfoByName(roomName,floorNo);
            if(null!=exitRoom){
                result.put("code","1003");
                result.put("codeDesc","该楼层名称已存在！");
                return result;
            }
            room.setTellerNo(user.getUserNo()+"");
            room.setRoomState("1");
            room.setCreateTime(Constance.currentDateTime());
            roomMapper.insert(room);
            result.put("code","0000");
            result.put("codeDesc","添加成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","添加失败！");
            return result;
        }
    }

    @Override
    public JSONObject queryRoomDetails(String roomNo) throws Exception {
        JSONObject result = new JSONObject();
        YlRoom room = roomMapper.selectById(roomNo);
        if(null!=room){
            result.put("name",room.getRoomName());
            result.put("type",room.getRoomType());
            result.put("remark",room.getRoomRemark());
            result.put("roomNo",room.getRoomNo());
            //判断该房间是否入住人
            int bedCount =bedMapper.queryBedCountByRoomNo(roomNo,"1");
            if(bedCount>0){
                result.put("uploadFlag","0");
            }else {
                result.put("uploadFlag","1");
            }
        }
        return result;
    }

    @Override
    public JSONObject updateRoomInfo(YlRoom room) throws Exception {
        JSONObject result = new JSONObject();
        if(null==room){
            result.put("code","1001");
            result.put("codeDesc","房间信息为空");
            return result;
        }
        try {
            //查询原楼层信息
            YlRoom oldRoom = roomMapper.selectById(room.getRoomNo());
            if(null==oldRoom){
                result.put("code","1002");
                result.put("codeDesc","房间信息不存在！");
                return result;
            }
            String oldRoomName = Constance.Null2EmptyTrim(oldRoom.getRoomName());
            String newRoomName = Constance.Null2EmptyTrim(room.getRoomName());
            if(!oldRoomName.equals(newRoomName)){
                //验证楼层编号是否重复
                YlRoom exitRoom = roomMapper
                        .queryRoomInfoByName(newRoomName,oldRoom.getFloorNo()+"");
                if(null!=exitRoom){
                    result.put("code","1003");
                    result.put("codeDesc","该房间名称已存在！");
                    return result;
                }
            }
            oldRoom.setRoomName(room.getRoomName());
            oldRoom.setRoomType(room.getRoomType());
            oldRoom.setRoomRemark(room.getRoomRemark());
            roomMapper.updateById(oldRoom);
            result.put("floorNo",oldRoom.getFloorNo());
            result.put("code","0000");
            result.put("codeDesc","修改成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","修改失败！");
            return result;
        }
    }

    @Override
    public JSONObject deleteRoomInfo(String roomNo) throws Exception {
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(roomNo)){
            result.put("code","1001");
            result.put("codeDesc","房间编号为空！");
            return result;
        }
        try {
            //查询原楼层信息
            YlRoom oldRoom = roomMapper.selectById(roomNo);
            if(null==oldRoom){
                result.put("code","1002");
                result.put("codeDesc","房间信息不存在！");
                return result;
            }
            //查询该房间已入住人数
            int bedCount =bedMapper.queryUserBendCount(roomNo);
            if(bedCount>0){
                result.put("code","1003");
                result.put("codeDesc","该房间存在已使用的床位，无法删除！");
                return result;
            }
            oldRoom.setRoomState("9");
            //删除该房间下的所有床位信息
            bedMapper.deleteBedInfoByRoomNo(roomNo);
            roomMapper.updateById(oldRoom);
            result.put("code","0000");
            result.put("codeDesc","删除成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","删除失败");
            return result;
        }
    }
}
